package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.SysOptions;
import cn.beyond.vehicleManagement.service.DictionaryService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {"/DictionaryServlet"})
public class DictionaryServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        DictionaryService service=new DictionaryService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SysOptions sysOptions=new SysOptions();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<SysOptions> sysOptionsList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
//            case "addvehicleProcurement":
//                boolean addflag= addUser(req,userService);
//                out.print(addflag);
//                break;
            //删除数据字典
            case "deleteDictionary":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.deleteDictionary(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
//            case "updatevehicleProcurement":
//                updateUser(req,userService,out);
//
//                break;
            case "selectDictionary":
                 String type=req.getParameter("type");
                 String value=req.getParameter("value");
                 map.put("type_Id",type);
                 map.put("value",value);
                 sysOptions=service.selectDictionaryByOne(sysOptions,map);
                 String optionJson= JSON.toJSONString(sysOptions);
                 out.write(optionJson);
                 map.clear();
                break;
            case "listDictionary":
                //列举出数据字典列表
                int startRow= Integer.parseInt(req.getParameter("startRow"));
                int pageSize= Integer.parseInt(req.getParameter("pageSize"));
                int totals= PageUtil.getPageCount("sys_options");
                int totalpage=totals%pageSize==0?(totals/pageSize):(totals/pageSize+1);
                List<Integer>totalinfo=new ArrayList<>();
                totalinfo.add(totals);
                totalinfo.add(totalpage);
                sysOptionsList= PageUtil.getPageUtil(startRow,pageSize,"sys_options",sysOptions);
                //sysOptionsList=service.selectUserByCondition(sysOptions,map);
                List<Object>listListMap=new ArrayList<>();
                listListMap.add(totalinfo);
                listListMap.add(sysOptionsList);
                String listJosn= JSON.toJSONString(listListMap);
                out.write(listJosn);
                map.clear();
                break;

            case "DictionaryList":
                String username=req.getParameter("username");
                req.getSession().setAttribute("username",username);
                sysOptionsList=service.selectUserByCondition(sysOptions,map);
                ServletContext application=this.getServletContext();
                application.setAttribute("optionlist",sysOptionsList);
                System.out.println(sysOptionsList);
               // req.getRequestDispatcher("index.jsp").forward(req,resp);
                resp.sendRedirect("index.jsp");
                map.clear();

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
